package com.nuc.bigdata.api.controller;

import com.nuc.bigdata.annotation.ResponseResult;
import com.nuc.bigdata.app.service.CommodityService;
import com.nuc.bigdata.domain.vo.CommentsVO;
import com.nuc.bigdata.domain.vo.CommodityVO;
import com.nuc.bigdata.feign.dto.NewsCommentsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @author JackLuo
 * @date 2019/11/28
 */
@RestController
@RequestMapping("/front")
@CrossOrigin
public class CommodityController {

    private final CommodityService commodityService;

    @Autowired
    CommodityController(CommodityService commodityService) {
        this.commodityService = commodityService;
    }

    /**
     * 获取所有商品分页
     * @param pageSize 页码
     * @return 这里经过ResponseResult注解包装，返回的是Result类型，为了能兼容Service返回类型，就使用了Object类型
     */
    @GetMapping("/goods")
    @ResponseResult
    public Object listAllCommodity(@RequestParam("pageSize")Long pageSize) {
        return commodityService.listAll(pageSize);
    }

    /**
     * 根据商品唯一标识获取商品详情
     * @param commodityId 商品唯一标识
     * @return 商品VO
     */
    @GetMapping("/goods/{commodityId}")
    @ResponseResult
    public Object queryCommodityById(@PathVariable("commodityId")String commodityId) {
        return commodityService.queryDetailById(commodityId);
    }

    /**
     * 根据商品唯一标识获取商品图文详情
     * @param commodityId 商品唯一标识
     * @return 包含商品图文详情的VO
     */
    @GetMapping("/goods/{commodityId}/picAndDesc")
    @ResponseResult
    public Object queryCommodityPicAndDesc(@PathVariable("commodityId")String commodityId) {
        return commodityService.queryPicAndDescById(commodityId);
    }

    @GetMapping("/goods/{commodityId}/comments")
    @ResponseResult
    public Object listByCommodityId(@PathVariable("commodityId")String commodityId,
                                                   @RequestParam("pageSize")Long pageSize) {
        return commodityService.listByCommodityId(commodityId, pageSize);
    }

    @PostMapping("/goods/{commodityId}/comments")
    @ResponseResult
    public Object createComment(@PathVariable("commodityId")String commodityId,
                                NewsCommentsDto commentsDto) {
        commodityService.createComment(commodityId, commentsDto);
        return null;
    }

    @PostMapping("/goods/{commodityId}/comments/{commodityCommentId}/like")
    @ResponseResult
    public Object toCommentLike(@PathVariable("commodityCommentId")String commodityCommentId,
                                String userId) {
        commodityService.toCommentLike(commodityCommentId, userId);
        return null;
    }
}
